package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;

import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.SherePrice;
import com.ruoyi.system.domain.SysUserScok;
import com.ruoyi.system.service.SherePriceService;
import com.ruoyi.system.mapper.SherePriceMapper;
import com.ruoyi.system.service.SysUserScokService;
import com.ruoyi.system.utils.TCPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;


/**
* @author 20261
* @description 针对表【shere_price】的数据库操作Service实现
* @createDate 2024-06-21 17:33:30
*/
@Service
public class SherePriceServiceImpl implements SherePriceService{
    @Resource
    private SherePriceMapper sherePriceMapper;
    @Autowired
    private SysUserScokService sysUserScokService;
    @Override
    public List<SherePrice> getList(String code) {
        List<SherePrice> list = sherePriceMapper.getList(code);
        return list;
    }

    @Override
    public void addSharePrice(SherePrice sherePrice) {
        sherePriceMapper.addSharePrice(sherePrice);
    }

    @Override
    public List<SherePrice> list() {
        return sherePriceMapper.list();
    }

    @Override
    public Boolean buy(Integer num, String scokname, BigDecimal price, String scokId) {
        if (num==null||num==0){
            return false;
        }
        LoginUser loginUser = getLoginUser();
        Long userid = loginUser.getUserid();
        String username = loginUser.getUsername();
        System.out.println("------------"+userid);
        SysUserScok myScok=sysUserScokService.selectById(userid,scokId);

        boolean b = StringUtils.isNotNull(myScok);
        if (b){
            BigDecimal chengjiao = new BigDecimal(num).multiply(price);
            myScok.setScokPrice(myScok.getScokPrice().add(chengjiao));
            myScok.setNum(myScok.getNum()+num);
            myScok.setIndate(DateUtils.getNowDate());
            boolean aa= sysUserScokService.updateByScok(myScok);
            return aa;
        }
        else {
            SysUserScok sysUserScok = new SysUserScok();
            BigDecimal chengjiao = new BigDecimal(num).multiply(price);
            sysUserScok.setScokPrice(chengjiao);
            sysUserScok.setIndate(new Date());
            sysUserScok.setScokName(scokname);
            sysUserScok.setUserId(userid.intValue());
            sysUserScok.setUname(username);
            sysUserScok.setStatus(0);
            sysUserScok.setNum(num);
            sysUserScok.setScokId(scokId);
            boolean aa = sysUserScokService.updateById(sysUserScok);
            return aa;
        }
    }

    @Override
    public List<SherePrice> getCode(String code) {
        return sherePriceMapper.getCode(code);
    }
}




